﻿
using System;
using System.Collections.Generic;
using System.Text;

using YouHP.Utility;
using YouHP.BLL;
using YouHP.Model;
using YouHP.BLL.MedicalGraph;

namespace YouHP.Pages.Profile
{
    public class IndicatorAjax : BasePage
    {

        protected override void ShowPage()
        {
            base.ShowPage();
            if (ErrorCount > 0) return;

            if (IsPost == false) HandleGet();
        }

        protected void HandleGet()
        {
            int id = RequestTools.GetQueryInt("id", 0);
            if (id <= 0) return;
            PatientRecordInfo prData = PatientBLL.Instance.GetPatientRecordInfo(id);
            if (prData.Id <= 0) return;
            MakeGraphData(prData);
        }

        private void MakeGraphData(PatientRecordInfo prData)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("&Indicator=1&\r\n");
            sb.AppendFormat("&StartDate={0:yyyy-MM-dd}&\r\n", prData.StartDate);
            sb.AppendFormat("&DosageDate={0:yyyy-MM-dd}&\r\n", prData.ConfirmDate);
            sb.AppendFormat("&UpdateDate={0:yyyy-MM-dd}&\r\n", prData.UpdateDate);

            List<DiseaseLabcheckInfo> indicatorLst = DiseaseBLL.Instance.GetDiseaseLabcheckList(prData.DiseaseId);
            Dictionary<int, int> dict = new Dictionary<int, int>();

            foreach (DiseaseLabcheckInfo info in indicatorLst)
            {
               

                if (dict.ContainsKey(info.ChartIndex) == false)
                {
                    dict.Add(info.ChartIndex, 0);
                }
                dict[info.ChartIndex] = Math.Max(dict[info.ChartIndex], info.AxisIndex);
                int count;
                List<IndicatorHistoryInfo> lst = PatientIndicatorBLL.Instance.GetIndicatorHistoryList(prData.Id, info.LabcheckId,100000,1,out count);

                sb.AppendFormat("&Var_{0}_{1}={2};{3};{4};{5}&\r\n",
                   info.ChartIndex, info.AxisIndex,
                   info.ShortName, info.LabcheckName,
                   info.LabcheckUnit,lst.Count
                   );
                sb.AppendFormat("&Grid_{0}_{1}={2}&\r\n",
                    info.ChartIndex, info.AxisIndex,
                    info.GridValues.Replace(",", ";")
                    );
                sb.AppendFormat("&Range_{0}_{1}={2}&\r\n",
                    info.ChartIndex, info.AxisIndex,
                    info.NormalValues.Replace("@", ";")
                    );

                for (int i = 0; i < lst.Count ; i++)
                {
                    IndicatorHistoryInfo item = lst[lst.Count - 1 - i];
                    sb.AppendFormat("&Data_{0}_{1}_{2}={3:yyyy-MM-dd};{4}&\r\n",
                        info.ChartIndex, info.AxisIndex, i+1,
                        item.RecordDate, item.DataValue);
                }
                sb.AppendLine();
            }
            foreach (KeyValuePair<int, int> item in dict)
            {
                sb.AppendFormat("&Chart_{0}={1}&\r\n", item.Key, item.Value);
            }
            sb.AppendFormat("&ChartCount={0}&\r\n", dict.Count);

            ResponseXML(sb);
        }

        private void ResponseXML(StringBuilder sb)
        {
            System.Web.HttpContext.Current.Response.Clear();
            System.Web.HttpContext.Current.Response.ContentType = "Text/html";
            System.Web.HttpContext.Current.Response.Expires = 0;

            System.Web.HttpContext.Current.Response.Write(sb.ToString());
            System.Web.HttpContext.Current.Response.End();
        }
    }
}